package com.typhoon.spring_jdbctemplate.util;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.typhoon.spring_jdbctemplate.dao.support.CustomJdbcTemplate;

/**
 * 简化数据源配置类
 * 
 * @author Typhoon
 * @date 2017-08-22 11:27 Tuesday
 * @since V1.3.1
 */
public class DataSourceUtils {

    public static Properties p       = null;
    public static String     confile = "jdbc.properties";

    static {
        p = new Properties();
        InputStream inputStream = null;
        try {
            inputStream = DataSourceUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            inputStream = new BufferedInputStream(inputStream);
            p.load(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static DataSource getDataSource() {
        try {
            return DruidDataSourceFactory.createDataSource(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JdbcTemplate getJdbcTemplate() {
        DataSource   dataSource   = getDataSource();
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        return jdbcTemplate;
    }
    
    /**
     * 获取自定义jdbcTemplate
     * 
     * @return
     */
    public static CustomJdbcTemplate getCustomJdbcTemplate() {
        DataSource   dataSource   = getDataSource();
        CustomJdbcTemplate customJdbcTemplate = new CustomJdbcTemplate(dataSource);
        return customJdbcTemplate;
    }
    
    public static NamedParameterJdbcTemplate getNamedJdbcTemplate() {
    	 DataSource   dataSource   = getDataSource();
    	 NamedParameterJdbcTemplate namedJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    	 return namedJdbcTemplate;
    }

    public static void main(String[] args) {
        JdbcTemplate jdbcTemplate = getJdbcTemplate();
//        String sql = " select * from User ";
//        List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
//        for(Map<String,Object> map : list) {
//        	System.out.println(map.toString());
//        }
//        jdbcTemplate.batchUpdate(sql, batchArgs)
//        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
//			@Override
//			public void setValues(PreparedStatement ps, int i) throws SQLException {
//			}
//			@Override
//			public int getBatchSize() {
//				return 0;
//			}
//		});
//    	NamedParameterJdbcTemplate namedJdbcTemplate = getNamedJdbcTemplate();
//    	MapSqlParameterSource parameterSource = new MapSqlParameterSource();
//    	parameterSource.addValue("name", "aeolus");
//    	String sql = " select * from User where name = :name  ";
//    	List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sql, parameterSource);
//    	for(Map<String,Object> map : list) {
//    	System.out.println(map.toString());
//    	}
//    	List<User> users = namedJdbcTemplate.query(sql, parameterSource, BeanPropertyRowMapper.newInstance(User.class));
//    	for(User u : users) {
//    		System.out.println(u);
//    	}
    	//User u = namedJdbcTemplate.queryForObject(sql, parameterSource, BeanPropertyRowMapper.newInstance(User.class));
    	//System.out.println(u);
    	
    	
//        List<Object[]> list = new ArrayList<>(10);
//        Object[] arr = null;
//        Calendar cal = Calendar.getInstance();
//        for(int i = 0; i < 10;i ++) {
//        	arr = new Object[2];
//        	arr[0] = "scorpio" + (i + 1);
//        	arr[1] = cal.getTime();
//        	
//        	list.add(arr);
//        }
//        long start = System.currentTimeMillis();
//        jdbcTemplate.batchUpdate("  insert into User values(null,?,? ) ", list);
//        long time = System.currentTimeMillis() - start;
//        System.out.println("耗时:" + time);
//        
        
        final List<Object[]> list = new ArrayList<>(10);
        Object[] arr = null;
        Calendar cal = Calendar.getInstance();
        for(int i = 0; i < 10;i ++) {
        	arr = new Object[2];
        	arr[0] = "scorpio" + (i + 1);
        	arr[1] = cal.getTime();
        	
        	list.add(arr);
        }
        long start = System.currentTimeMillis();
        jdbcTemplate.batchUpdate("  insert into User values(null,?,? ) ", new BatchPreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				Object[] params = list.get(i);
				ps.setString(1, params[0].toString());
				long createTime = ((Date)params[1]).getTime();
				ps.setDate(2, new java.sql.Date(createTime));
			}
			@Override
			public int getBatchSize() {
				return list.size();
			}
		});
        long time = System.currentTimeMillis() - start;
        System.out.println("耗时:" + time);
        
        
        
    	
    }
}
